<?php
/*******************************************************************************
index.php
primary processing and display page

Created by Jason White (jbwhite@emory.edu)
Modified by NCSU Libraries, NC State University. Modifications by Karl Doerr & Troy Hurteau (libraries.opensource@ncsu.edu).

This file is part of NCSU's distribution of ReservesDirect. This version has not been downloaded from Emory University
or the original developers of ReservesDirect. Neither Emory University nor the original developers of ReservesDirect have authorized
or otherwise endorsed or approved this distribution of the software.

Copyright (c) 2004-2006 Emory University, Atlanta, Georgia.

Licensed under the NCSU ReservesDirect License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. You may obtain a copy of the full License at
 http://www.lib.ncsu.edu/it/opensource/

ReservesDirect is distributed in the hope that it will be useful,
but is distributed "AS IS" and WITHOUT ANY WARRANTY, without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE, and without any warranty as to non-infringement of any third
party's rights. See the License for the specific language governing permissions and limitations under the License.

The original version of ReservesDirect is located at:
http://www.reservesdirect.org/

This version of ReservesDirect, distributed by NCSU, is located at:
http://code.google.com/p/reservesdirect-ncsu/

*******************************************************************************/
if(file_exists('localize.php')){
	require_once('localize.php');
}

require_once('DefineLoad.php');
require_once('FileExistsInPath.php');

// Define application version
defineLoad('APPLICATION_STATUS', 'online');

// Define application version
defineLoad('APPLICATION_VERSION', '1.13');

// Define application environment
defineLoad('APPLICATION_ENV', 'production');

// Define path to application directory
defineLoad('ROOT_PATH', realpath(dirname(__FILE__)));

// Define path to application directory
defineLoad('APPLICATION_PATH', ROOT_PATH . '/secure');

// Define application configuration location
defineLoad('APPLICATION_CONF', '/var/www/reserves/admin/configs/config.xml');

$load_start_time = time();

require_once(APPLICATION_PATH . '/headers.inc.php');
require_once(APPLICATION_PATH . '/config.inc.php');
require_once(APPLICATION_PATH . '/common.inc.php');
require_once(APPLICATION_PATH . '/startup.inc.php');

$defaultCommand = 'viewCourseList';
$adminUser = null;
$mobile = false;

$cmd = (
	array_key_exists('cmd', $_REQUEST) && '' != trim($_REQUEST['cmd']) 
	? $_REQUEST['cmd'] 
	: $defaultCommand
);

//test user permissions
if (!key_exists($cmd, $functional_permissions) 
	|| ($u->getRole() < $functional_permissions[$cmd])
){ //user does not have permission kick to default page
	$cmd = $defaultCommand;
	if ($_SESSION['debug']) {
		echo "<B>Failed Permission check</B><hr>\n";
	}
}

if ((isset($_REQUEST['mobile']) && $_REQUEST['mobile'] == "true") || (isset($_SESSION['mobile']) && $_SESSION['mobile'] == "true")) {
	$_SESSION['mobile'] = 'true';
	$mobile = true;
}

if (isset($_REQUEST['u']))
{
	 $tmpUser = new user;
	 $tmpUser->getUserByID($_REQUEST['u']);

	 $adminUser = $usersObject->initUser($tmpUser->getUserClass(), $tmpUser->getUsername());

	 unset($tmpUser);
}

//if selected set CourseInstance
if (!empty($_REQUEST['ci']))
{
	$ci = new courseInstance($_REQUEST['ci']);
	$ci->getCourseForUser();
} else
	$ci = null;


//Force user to update email address
if (($u->getEmail() == "" || $u->getLastName() == "") && !isset($_REQUEST['edit_user_submit'])) //direct user to edit profile
{
	 $cmd = 'newProfile';
}

//initiate calendar object, since some files must be included in the <head> by one of the html includes
//this object should be global and used by all files (no need to create a new obj)
$calendar = new Calendar();


//if there is a command to delete a note, do it
if(!empty($_REQUEST['deleteNote'])) {
	noteManager::deleteNote($_REQUEST['deleteNote']);
}

if ($_SESSION['debug']) {
	echo "cmd=$cmd<hr>";
}

switch ($cmd)
{
	case 'viewReservesList': // myReserves Reserve List
	case 'previewReservesList':
	case 'previewStudentView';
	case 'sortReserves':
	case 'customSort':
	case 'selectInstructor': //addReserve Staff Interface - Search for Class by Instructor or Dept
	case 'addReserve': //add a reserve to a class
	case 'addMultipleReserves': //add a reserve to a class
	case 'searchScreen': //addReserve - Search for Item
	case 'searchResults': //addReserve - Search Results Screen
	case 'storeReserve': //addReserve - Store Reserves Screen
	case 'uploadDocument': //addReserve - upload Document Screen
	case 'addURL': //addReserve - add a URL screen
	case 'placeRequest': // addReserve - instructor enters a request into the processing queue
	case 'storeUploaded': //addReserve page - Store uploaded document
	case 'faxReserve': //addReserve - Fax Reserve Screen
	case 'getFax': //addReserve - Claim Fax Screen
	case 'addFaxMetadata': //addReserve - Fax Meta Data Screen
	case 'storeFaxMetadata': //addReserve - Store Fax Meta Data Screen
	case 'editMultipleReserves':	//edit common reserve data for multiple reserves in a class
		require_once(APPLICATION_PATH . '/managers/reservesManager.class.php');
		$mgr = new reservesManager($cmd, $u);
	break;


	case 'myReserves':
	case 'viewCourseList':
	case 'activateClass':
	case 'deactivateClass':
	case 'manageClasses':
	case 'editProxies':
	case 'editInstructors':
	case 'editCrossListings':
	case 'editTitle':
	case 'editClass':			// manageClass edit class
	case 'createClass':			// manageClass create class (enter meta-data)
	case 'createNewClass':		// manageClass create class (store meta-data to DB)
	case 'addClass':			// myReserves - add a class as a student
	case 'removeClass':			// myReserves - remove a class you are a student in
	case 'deleteClass':
	case 'confirmDeleteClass':
	case 'deleteClassSuccess':
	case 'copyItems':
	case 'processCopyItems':
		require_once(APPLICATION_PATH . '/managers/classManager.class.php');
		$mgr = new classManager($cmd, $u, $adminUser, $_REQUEST);
	break;

	case 'manageUser':
	case 'newProfile':
	case 'editProfile':
	case 'editUser':
	case 'mergeUsers':
	case 'addUser':
	case 'assignProxy':
	case 'assignInstr':
	case 'setPwd':
	case 'resetPwd':
	case 'removePwd':
	case 'addProxy':
	case 'removeProxy':
		require_once(APPLICATION_PATH . '/managers/userManager.class.php');
		$mgr = new userManager($cmd, $u, $adminUser);
	break;

	case 'editItem':
	case 'editHeading':
	case 'processHeading':
	case 'duplicateReserve';
		require_once(APPLICATION_PATH . '/managers/itemManager.class.php');
		$mgr = new itemManager($cmd, $u);
	break;

	case 'displayRequest':
	case 'setStatus':
	case 'storeRequest':
	case 'deleteRequest':
	case 'printRequest':
	case 'addDigitalItem':
	case 'addPhysicalItem':
	case 'addVideoItem':
		require_once(APPLICATION_PATH . '/managers/requestManager.class.php');
		$mgr = new requestManager($cmd, $u, $ci, $_REQUEST);
	break;

	case 'copyClass':
	case 'copyClassOptions':
	case 'copyExisting':
	case 'copyNew':
	case 'importClass':			//import reserves list from one ci to another
	case 'processCopyClass':
		require_once(APPLICATION_PATH . '/managers/copyClassManager.class.php');
		$mgr = new copyClassManager($cmd, $u, $_REQUEST);
	break;

	case 'addNote':
	case 'saveNote':
		require_once(APPLICATION_PATH . '/managers/noteManager.class.php');
		$mgr = new noteManager($cmd, $u);
	break;

	case 'exportClass':
	case 'generateBB':
		require_once(APPLICATION_PATH . '/managers/exportManager.class.php');
		$mgr = new exportManager($cmd);
	break;

	case 'searchTab':
	case 'doSearch':
	case 'addResultsToClass':
		require_once(APPLICATION_PATH . '/managers/searchManager.class.php');
		$mgr = new searchManager($cmd, $u, $_REQUEST);
	break;

	case 'reportsTab':
	case 'viewReport':
		$page = 'reports';
		require_once(APPLICATION_PATH . '/managers/reportManager.class.php');
		$mgr = new reportManager($cmd, $u, $_REQUEST);
	break;

	case 'admin':
	case 'testMxe':
		$page = 'admin';
		require_once(APPLICATION_PATH . '/managers/adminManager.class.php');
		$mgr = new adminManager($cmd, $u, $_REQUEST);
	break;

	case 'help':
	case 'helpViewArticle':
	case 'helpEditArticle':
	case 'helpViewCategory':
	case 'helpEditCategory':
	case 'helpViewTag':
	case 'helpSearch':	
	case 'helpSetRelated':
		$page = 'help';
		require_once(APPLICATION_PATH . '/managers/helpManager.class.php');
		$mgr = new helpManager($cmd);
	break;

	default:
		$page = '404';
		$cmd = 'notFound';
		require_once(APPLICATION_PATH . '/managers/errorManager.php');
		$mgr = new errorManager($cmd);
}

if (method_exists($mgr,'getDelegate')) { // #TODO make this more automatic and seamless.
   $mgr = $mgr->getDelegate();
} 

if ($mobile)
	require_once APPLICATION_PATH . '/html/mobile.inc.html';
elseif (isset($_REQUEST['no_control']) && $_REQUEST['no_control'] != 'false')
	require_once APPLICATION_PATH . '/html/no_control.inc.html';
elseif (isset($_REQUEST['no_table']) && $_REQUEST['no_table'] != 'false')
	require_once APPLICATION_PATH . '/html/no_table.inc.html';
else
	require_once APPLICATION_PATH . '/html/index.inc.html';

if (isset($_SESSION) && array_key_exists('debug', $_SESSION) && $_SESSION['debug'])
{
	$load_end_time = time();
	$load_time = $load_end_time - $load_start_time;
	print( "<br>this page took {$load_time} seconds to load");
} else if($debugMode){
	print('<a href="?debug=true">Enable Debugging</a>');
}
